package cn.iocoder.yudao.module.hs.controller.admin.materiallog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.hs.controller.admin.materiallog.vo.*;
import cn.iocoder.yudao.module.hs.dal.dataobject.materiallog.MaterialLogDO;
import cn.iocoder.yudao.module.hs.service.materiallog.MaterialLogService;

@Tag(name = "管理后台 - 物料日志")
@RestController
@RequestMapping("/hs/material-log")
@Validated
public class MaterialLogController {

    @Resource
    private MaterialLogService materialLogService;

    @PostMapping("/create")
    @Operation(summary = "创建物料日志")
    @PreAuthorize("@ss.hasPermission('hs:material-log:create')")
    public CommonResult<Long> createMaterialLog(@Valid @RequestBody MaterialLogSaveReqVO createReqVO) {
        return success(materialLogService.createMaterialLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料日志")
    @PreAuthorize("@ss.hasPermission('hs:material-log:update')")
    public CommonResult<Boolean> updateMaterialLog(@Valid @RequestBody MaterialLogSaveReqVO updateReqVO) {
        materialLogService.updateMaterialLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:material-log:delete')")
    public CommonResult<Boolean> deleteMaterialLog(@RequestParam("id") Long id) {
        materialLogService.deleteMaterialLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:material-log:query')")
    public CommonResult<MaterialLogRespVO> getMaterialLog(@RequestParam("id") Long id) {
        MaterialLogDO materialLog = materialLogService.getMaterialLog(id);
        return success(BeanUtils.toBean(materialLog, MaterialLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料日志分页")
    @PreAuthorize("@ss.hasPermission('hs:material-log:query')")
    public CommonResult<PageResult<MaterialLogRespVO>> getMaterialLogPage(@Valid MaterialLogPageReqVO pageReqVO) {
        PageResult<MaterialLogDO> pageResult = materialLogService.getMaterialLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料日志 Excel")
    @PreAuthorize("@ss.hasPermission('hs:material-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialLogExcel(@Valid MaterialLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialLogDO> list = materialLogService.getMaterialLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料日志.xls", "数据", MaterialLogRespVO.class,
                        BeanUtils.toBean(list, MaterialLogRespVO.class));
    }

}